home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------*/
- /* The Opus Computer-Based Conversation System */
- /* (c) Copyright 1986, Wynn Wagner III, All Rights Reserved */
- /*--------------------------------------------------------------------------*/
- /* */
- /* STRIP_Z: Filter Control-Z (CP/M style end-of-file marker) from text file */
- /* */
- /* COMPILER: MicroSoft C, v4.0 */
- /* */
- /*--------------------------------------------------------------------------*/
-
- #include <io.h>
- #include <stdio.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <malloc.h>
-
- #include "legible.h"
-
- static char *BUFFER_ERROR = "\r\nFile buffer error. Unable to continue.\r\n$";
-
-
-
-
- /*--------------------------------------------------------------------------*/
- /* EXTENDED HELP */
- /*--------------------------------------------------------------------------*/
- void extended_help()
- begin
- bdos(9,"Strip_Z removes control Z characters from a test file.\r\n$");
- bdos(9,"First, it creates a backup file (`.BAK') of the program...\r\n$");
- bdos(9,"then does the filtering.\r\n\n$");
- bdos(9," USAGE: strip_z filename\r\n\n$");
- bdos(9," EXAMPLE: strip_z c:\\pascal\\files.bbs\r\n\n$");
- exit(1);
- end
-
-
- /*--------------------------------------------------------------------------*/
- /* STRMFE: Make File Extension */
- /*--------------------------------------------------------------------------*/
- void strmfe( new, old, extension )
- char *new, *old, *extension;
- begin
- register int i;
-
- strcpy( new, old );
-
- for(i=0;i<79;i++)
- if (!new[i])
- begin
- new[i] = '.';
- new[i+1] = '\0';
- goto done;
- end
- else if (new[i]=='.')
- begin
- new[i+1] = '\0';
- goto done;
- end
- done:
- strcat( new, extension );
- end /* strmfe */
-
-
-
-
- /*--------------------------------------------------------------------------*/
- /* MAIN: Strip_Z */
- /*--------------------------------------------------------------------------*/
- main(argc,argv)
- int argc;
- char *argv[];
- begin
- register int c;
-
- FILE *source;
- FILE *dest;
-
- char *source_buffer;
- char *dest_buffer;
-
- char backup[82];
- struct stat *statptr;
-
-
-
- bdos(9,"STRIP_Z[1.0]\r\n$");
- if (argc<2) extended_help();
-
- /*--------------------------------------------------------------------*/
- /* See if the requested file exists. */
- /*--------------------------------------------------------------------*/
- statptr = (struct stat *)malloc( sizeof(struct stat) );
- if (stat(argv[1],statptr))
- begin
- free( statptr );
- bdos(9,"\r\n\nNO SUCH FILE\r\n$");
- extended_help();
- end
- free( statptr );
-
-
- /*--------------------------------------------------------------------*/
- /* Maintain a backup copy of the file. */
- /*--------------------------------------------------------------------*/
- strmfe( backup, argv[1], "Bak" );
- unlink( backup );
- if (rename(argv[1],backup))
- begin
- bdos(9,"\r\n\nCAN'T CREATE BACKUP\r\n$");
- extended_help();
- end
-
-
- /*--------------------------------------------------------------------*/
- /* Open the source and destination files */
- /*--------------------------------------------------------------------*/
- source = fopen( backup, "rb" );
- if (!source)
- begin
- bdos(9,"\r\nCAN'T OPEN SOURCE FILE\r\n$");
- extended_help();
- end
-
- dest = fopen( argv[1], "wb" );
- if (!dest)
- begin
- bdos(9,"\r\nCAN'T OPEN DESTINATION FILE\r\n$");
- extended_help();
- end
-
-
- /*--------------------------------------------------------------------*/
- /* Setup some elephant buffers */
- /*--------------------------------------------------------------------*/
- c = _memavl() / 3;
- source_buffer = malloc(c);
- dest_buffer = malloc(c);
-
- if ((!source_buffer) or (!dest_buffer))
- begin
- bdos(9,"\r\nMEMORY ERROR: unable to continue\r\n$");
- exit(1);
- end
-
- if (setvbuf(source,source_buffer,_IOFBF,c))
- begin
- bdos(9,BUFFER_ERROR);
- exit(1);
- end
-
- if (setvbuf(dest,dest_buffer,_IOFBF,c))
- begin
- bdos(9,BUFFER_ERROR);
- exit(1);
- end
-
-
-
- /*--------------------------------------------------------------------*/
- /* Copy the file */
- /*--------------------------------------------------------------------*/
- while(1)
- begin
- switch( c=getc(source))
- begin
-
- case EOF : fclose(source);
- fclose(dest);
- free(source_buffer);
- free(dest_buffer);
- exit(0);
-
- case 0x1a : break;
-
- default : putc( c, dest );
-
- end /* switch */
- end
-
-
- end
-
-
- /* END OF FILE: strip_z.c */
-